package com.wwdx.controller.rest;

import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.wwdx.controller.base.BaseWxPayController;
import com.wwdx.model.WwdxOrderModel;
import com.wwdx.model.dto.BasePayDTO;
import com.wwdx.model.dto.ResultBean;
import com.wwdx.service.impl.BaseWxPayService;
import com.wwdx.service.impl.WxAppPayService;
import com.wwdx.utils.ConstantHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.SortedMap;

/**
 * @author liguoliang
 */
@RestController
@RequestMapping("wxpay")
@Api(tags = "APP微信支付接口")
public class RestAppPayController extends BaseWxPayController{

    @Resource
    private WxAppPayService wxAppPayService;

    @Override
    public BaseWxPayService getPayService() {
        return this.wxAppPayService;
    }

    @Override
    public String getPayName() {
        return "wxpay";
    }

    @Override
    protected Integer getPayType() {
        return ConstantHolder.Constant.WECHAT_APP_PAYTYPE;
    }

    /**
     * 保存微信订单
     */
    @RequestMapping("/wxsave")
    @ApiOperation(value = "APP微信支付",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",required = true),
            @ApiImplicitParam(name = "sign",value = "签名",required = true),
            @ApiImplicitParam(name = "type",value = "产品类型",required = true),
            @ApiImplicitParam(name = "product_id",value = "产品ID",required = true),
            @ApiImplicitParam(name = "channel",value = "渠道",required = true)
    })
    @Override
    public ResultBean wxsave(String token, String sign,Integer type, String unionId, String channel,
                             String openId, @RequestParam("product_id") Long productId, HttpServletRequest request) {

        SortedMap<String, Object> sortedMap = super.getSortedMap(token,sign);
        sortedMap.put("product_id", productId);
        ResultBean resultBean = super.validSignTokenResult(sortedMap);
        if (resultBean.getStatus() == 1){
            return resultBean;
        }
        WwdxOrderModel model = WwdxOrderModel.builder().payType(this.getPayType()).type(type)
                .product_id(productId).channel(channel).build();
        logger.info("APP微信支付:{}",model);
        unionId = (String) resultBean.getData();
        model.setUnionid(unionId);
        BasePayDTO.PayModel payModel = payService.save(model, super.makeOrderNum());
        if (StringUtils.isNotBlank(payModel.getError())) {
            return renderError(payModel.getError());
        }
        return super.execute(request,model,payModel, WxPayConstants.TradeType.APP);
    }
}